package com.picture.service.impl;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.picture.constant.MessageConstant;
import com.picture.context.BaseContext;
import com.picture.mapper.PictureMapper;
import com.picture.mapper.PictureTagMapper;
import com.picture.mapper.UserMapper;
import com.picture.mapper.UserPictureMapper;
import com.picture.pojo.dto.PicturePageQueryDTO;
import com.picture.pojo.dto.PictureSaveDto;
import com.picture.pojo.dto.PictureStatusQueryDto;
import com.picture.pojo.entity.Picture;

import com.picture.pojo.entity.PictureTag;
import com.picture.pojo.entity.User;
import com.picture.pojo.entity.UserPicture;
import com.picture.result.PageResult;
import com.picture.result.Result;
import com.picture.service.PictureService;
import com.picture.utils.AliOssUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 悦神
 * @description 针对表【picture】的数据库操作Service实现
 * @createDate 2024-03-01 09:07:17
 */
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture>
        implements PictureService {
    @Autowired
    PictureMapper pictureMapper;
    @Autowired
    PictureTagMapper pictureTagMapper;
    @Autowired
    UserMapper userMapper;

    @Autowired
    UserPictureMapper userPictureMapper;

    @Autowired
    AliOssUtil aliOssUtil;

    @Transactional
    @Override
    public Result saveWithPicture(List<PictureSaveDto> pictureSaveDto) {
        // 遍历图片保存实体类列表
        for (PictureSaveDto saveDto : pictureSaveDto) {
            // 如果描述为空，则抛出异常
            if (saveDto.getDescription().equals("") || saveDto.getDescription() == null) {
                throw new RuntimeException(MessageConstant.PICTURE_DESCRIPTION_NULL);
            }
            // 创建图片实体类
            Picture picture = new Picture();
            // BeanUtils.copyProperties(saveDto, picture);
            // 设置图片url
            picture.setPictureUrl(saveDto.getPictureUrl());
            // 设置图片分类
            picture.setAclass(saveDto.getAclass());
            // 设置图片分类id
            picture.setPictureCategoryId(saveDto.getPictureCategoryId());
            // 设置描述
            picture.setDescription(saveDto.getDescription().trim());
            // 设置图片状态
            picture.setPictureStatus(saveDto.getPictureType());

            // 设置更新时间
            picture.setUpdateTime(LocalDateTime.now());
            // 如果图片url为空，则抛出异常
            if (picture.getPictureUrl() == null || picture.getPictureUrl().equals("")) {
                throw new RuntimeException(MessageConstant.PICTURE_NULL);
            }
            // 如果图片分类id为空，则抛出异常
            if (picture.getPictureCategoryId() == null) {
                throw new RuntimeException(MessageConstant.PICTURE_CATEGORY_NULL);
            }
            // 如果图片状态为空，则设置为默认值
            if (picture.getPictureStatus() == null || picture.getPictureStatus().equals("")) {
                picture.setPictureStatus("0");
            }

            // 插入图片数据
            pictureMapper.insert(picture);
        }
        return Result.success();
    }

    @Override
    public Result<PageResult> picturelist(PicturePageQueryDTO picturePageQueryDTO) {
        // PageHelper.startPage(picturePageQueryDTO.getPage(),picturePageQueryDTO.getPageSize());
        //根据条件查询图片，并分页
        Page<Picture> picturePage = new Page<>(picturePageQueryDTO.getPage(), picturePageQueryDTO.getPageSize());
        //创建查询条件
        LambdaQueryWrapper<Picture> lqw1 = new LambdaQueryWrapper<Picture>();
        //根据图片类型查询
        lqw1.eq(!StringUtils.isEmpty(picturePageQueryDTO.getPictureType()), Picture::getPictureStatus, picturePageQueryDTO.getPictureType());
        //根据图片分类查询
        lqw1.eq(picturePageQueryDTO.getPictureCategoryId() != null, Picture::getPictureCategoryId, picturePageQueryDTO.getPictureCategoryId());
        //根据用户id查询
        lqw1.eq(picturePageQueryDTO.getABoolean(), Picture::getUserId, BaseContext.getCurrentId());
        //根据班级查询
        lqw1.like(!StringUtils.isEmpty(picturePageQueryDTO.getAClass()), Picture::getAclass, picturePageQueryDTO.getAClass());
        //根据图片描述查询
        lqw1.like(!StringUtils.isEmpty(picturePageQueryDTO.getKeys()), Picture::getDescription, picturePageQueryDTO.getKeys());
        //查询图片分页
        Page<Picture> picturePage1 = pictureMapper.selectPage(picturePage, lqw1);
        //获取总记录数
        long total = picturePage1.getTotal();
        //获取图片列表
        List<Picture> result = picturePage1.getRecords();
        //封装分页结果
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);

        return Result.success(pageResult);
    }

    @Override
    public Result<List<PictureTag>> getTag(Long id) {
        Picture picture = pictureMapper.selectById(id);
        if (picture == null) {
            throw new RuntimeException(MessageConstant.ROOT_DELETE);
        }
        LambdaQueryWrapper<PictureTag> lqw1 = new LambdaQueryWrapper<PictureTag>();
        lqw1.eq(PictureTag::getPictureId, id);
        List<PictureTag> pictureTags = pictureTagMapper.selectList(lqw1);
        return Result.success(pictureTags);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        User user = userMapper.selectById(BaseContext.getCurrentId());
        if (!user.getName().equals("root")) {
            throw new RuntimeException(MessageConstant.NOT_AUTHORITY);
        }
        String pictureUrl = pictureMapper.selectById(id).getPictureUrl();
        pictureMapper.deleteById(id);
        LambdaQueryWrapper<PictureTag> p = new LambdaQueryWrapper<>();
        p.eq(PictureTag::getPictureId, id);
        pictureTagMapper.delete(p);
        aliOssUtil.deleteFile(pictureUrl);
    }

    @Override
    public void exist(Long id) {
        Picture picture = pictureMapper.selectById(id);
        if (picture == null) {
            throw new RuntimeException(MessageConstant.ROOT_DELETE);
        }
    }

    @Override
    public void updatestatus(Long id) {
        Picture picture = pictureMapper.selectById(id);
        picture.setPictureStatus("2");
        pictureMapper.updateById(picture);
    }


}




